package com.tech.view.panel;

import com.tech.repository.entity.Port;
import com.tech.service.PortService;
import com.tech.service.impl.ServiceImplFactory;
import com.tech.util.LogProcessUtil;
import com.tech.view.common.GraphicsUtil;
import com.tech.view.common.ResourcesUtil;
import com.tech.view.common.NoMouseListenerComponentFactory;
import com.tech.view.component.panel.SlideshowPanel;

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.List;
import java.util.logging.Logger;

/**
 * 公告展示面板
 */
public class PortDisplayPanel extends ImageJPanel {
	private final static Logger LOG = LogProcessUtil.getLogger(PortDisplayPanel.class.getName());
	private int width = 860;
	private int height;

	private List<Port> portList;
	private SlideshowPanel slideshowPanel;

	private PortService portService;

	public PortDisplayPanel() {
		// 高度默认180
		this.height = 180;
		initService();
		initComponents();
		setListeners();
		initView();
	}

	private void initService() {
		portService = ServiceImplFactory.getPortService();
		portList = portService.getRecentPorts();
	}

	private void initComponents() {
		if (portList.size() == 0) return;

		List<JTextArea> areas = portList.stream()
				.map(port -> NoMouseListenerComponentFactory.getTextArea(port.getNtitle() + "\n" + port.getNcontent()))
				.toList();
		// 这里定义点击公告没有反应
		slideshowPanel = new SlideshowPanel(500, 130, 3000L, areas, null);
		slideshowPanel.setBounds((width - 500) / 2, (height - 130) / 2, 500, 130);
	}

	private void setListeners() {

	}

	private void initView() {
		setPreferredSize(new Dimension(width, height));
		setLayout(null);

		if (portList.size() != 0)
			add(slideshowPanel);

		// 设置背景
		BufferedImage background = GraphicsUtil.createScaledCompositeInstance(ResourcesUtil.getImage(ResourcesUtil.Image.PORT_BACKGROUND), 860, height);
		setBackgroundImage(background);
	}

	public void stopAnimation(){
		slideshowPanel.stopAnimation();
	}
}
